package Tools.Pathfinding;

import java.util.ArrayList;
import java.util.List;

import org.newdawn.slick.util.pathfinding.AStarPathFinder;
import org.newdawn.slick.util.pathfinding.Path;

import Game.MapBlock;
import GameMath.GPoint;
import GameMath.GSize;

/** Pathfinding class
 * @author Aikidos
 */
public class Pathfinding {
	/** Max search distance */
	static int MAX_SEARCH_DISTANCE = 100;

	static private PathfindingMap _pathfindingMap = null;
	
	/** Create new pathfinding map
	 * @param m - Level map
	 * @param mapSize - Map size
	 */
	static public void createNewPathfindingMap(MapBlock[][] m, GSize mapSize) {
		_pathfindingMap = new PathfindingMap(m, mapSize);
	}
	
	/** Returns pathfinding map
	 * @return Pathfinding map
	 */
	static public PathfindingMap getPathfindingMap() {
		return _pathfindingMap;
	}
	
	/** Find path
	 * @param map - Map
	 * @param start - Start position
	 * @param finish - Finish position
	 * @return List of walk point
	 */
	static public List<GPoint> find(GPoint start, GPoint finish) {
		AStarPathFinder pathfinder = new AStarPathFinder(_pathfindingMap, MAX_SEARCH_DISTANCE, false);
		Path path = pathfinder.findPath(null, start.x, start.y, finish.x, finish.y);
		
		if (path != null) {
			// Convert
			List<GPoint> ret = new ArrayList<GPoint>();
			for (int i = 0; i < path.getLength(); i++)
				ret.add(new GPoint(path.getStep(i).getX(), path.getStep(i).getY()));
			
			return ret;
		} else
			return null;
	}
}
